use std::{collections::HashMap, thread::sleep, time::Duration};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    let token = String::from("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyaWQiOiI1OTEyMDg4ZTQ4YmM0MGFjODQ2MjJiMzMwZTg0MTc1ZiIsInVuaW9uaWQiOiJvbXVSV3c4N2w1Q3ZacVhaZmFTbmVzRnVXZVBvIiwib3BlbmlkIjoib2VDTEo1a0FkM0d5RnN2bDhRWWF0WTZIS3RzRSIsImV4cCI6MTcxMDgxNDU5MTMwN30.eXKKXqklU0W9vKH1xuHqNu37clEbeqpebfF17jg1zZY");

    let _ = model_graph_peer::peer_start().await;

    let result = model_graph_peer::peer_login(&token).await;

    match result {
        Ok(result) => {
            tracing::debug!("peer login:{:?}", result);
        }
        Err(err) => tracing::error!("login fail:{:?}", err),
    }

    let remote_peer_id = String::from("12D3KooWBzNmYjd9Ac5AtGFWEC6ukufNXQYubG8djWvqWQwgRwHv");

    // let result = model_graph_peer::data_channels::prepare_follower_channel(&remote_peer_id).await;

    // match result {
    //     Ok(_) => {
    //         tracing::debug!("prepare_follower_channel 成功！");
    //     }
    //     Err(err) => tracing::debug!("===-----{:?}", err),
    // }
    // sleep(Duration::from_secs(10));

    //HTTP REQUEST
    for _ in 0..1000 {
        let request = model_graph_peer::data_channels::proxy::HttpRequest {
            url: String::from("http://localhost:8080/index.html"),
            method: model_graph_peer::data_channels::proxy::HttpMethod::Get,
            version: String::from("HTTP/1.1"),
            headers: HashMap::new(),
            extensions: HashMap::new(),
        };
        let result =
            model_graph_peer::data_channels::proxy::http_request(&remote_peer_id, request).await;
        match result {
            Ok(_) => {
                tracing::debug!("prepare_follower_channel 成功！");
            }
            Err(err) => tracing::debug!("===-----{:?}", err),
        }
        sleep(Duration::from_secs(3));
    }

    tokio::select! {
        _ = tokio::signal::ctrl_c() => {
            println!();
        }
    };
    tracing::info!("RRAI 结束...");
    Ok(())
}
